package com.aaa.tmr.service.impl;

import com.aaa.tmr.entity.Business;
import com.aaa.tmr.dao.BusinessDao;
import com.aaa.tmr.service.BusinessService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import javax.servlet.http.HttpSession;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Business)表服务实现类
 *
 * @author makejava
 * @since 2021-03-24 16:07:52
 */
@Service("businessService")
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessDao businessDao;
    @Resource
    private HttpSession session;

    /**
     * 通过ID查询单条数据
     *
     * @param businessId 主键
     * @return 实例对象
     */
    @Override
    public Business queryById(Integer businessId) {
        return this.businessDao.queryById(businessId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Business> queryAllByLimit(int offset, int limit) {
        return this.businessDao.queryAllByLimit(offset, limit);
    }
    /**
     * @return
     * @create by: ydq
     * @description: 方法作用：获取业务类型总数量
     * @create time: 2020/12/30 19:49
     * @param:
     */
    @Override
    public List<Long> businessEchart() {
        List<Map<String, Object>> list = businessDao.businessEchart();
        List<Long> list1 = new ArrayList<>();
        long patent = 0, trademark = 0, copyright = 0, property = 0;
        for (Map<String, Object> map : list) {
            String a = map.get("business_top_type").toString();
            switch (a) {
                case "专利":
                    patent = (Long) map.get("count(*)");
                    break;
                case "商标":
                    trademark = (Long) map.get("count(*)");
                    break;
                case "版权":
                    copyright = (Long) map.get("count(*)");
                    break;
                case "知识产权":
                    property = (Long) map.get("count(*)");
                    break;
            }
        }
        list1.add(patent);
        list1.add(trademark);
        list1.add(copyright);
        list1.add(property);
        return list1;
    }
    /**
     * @return
     * @create by: ydq
     * @description: 方法作用：业务分析按时间
     * @create time: 2020/12/30 20:52
     * @param:
     */
    @Override
    public Map<String, Object> showBusinessChartByAgent(String loginName) {
        List<Map<String, Object>> list = businessDao.showBusinessChartByAgent(loginName);
        Map<String, Object> map = new HashMap<>();
        //X轴
        String[] X = new String[list.size()];
        //y轴
        Long[] Y = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            X[i] = list.get(i).get("month").toString();
            Y[i] = (Long) list.get(i).get("count");
        }
        map.put("X", X);
        map.put("Y", Y);
        return map;
    }
    /**
     * @return
     * @create by: ydq
     * @description: 方法作用：业务分析按代理人
     * @create time: 2020/12/30 20:12
     * @param:
     */
    @Override
    public Map<String, Object> showAgentChart(String startTime, String endTime) {
        if (startTime == null || "".equals(startTime)) {
            startTime = "2000-01-01";
        }
        if (endTime == null || "".equals(endTime)) {
            endTime = "2100-01-01";
        }
        List<Map<String, Object>> list = businessDao.showAgentChart(startTime, endTime);
        Map<String, Object> map = new HashMap<>();
        //X轴
        String[] X = new String[list.size()];
        //y轴
        Long[] Y = new Long[list.size()];
        for (int i = 0; i < list.size(); i++) {
            X[i] = list.get(i).get("login_name").toString();
            Y[i] = (Long) list.get(i).get("count(*)");
        }
        map.put("X", X);
        map.put("Y", Y);
        return map;
    }

    /**
     * 新增数据
     *
     * @param business 实例对象
     * @return 实例对象
     */
    @Override
    public Business insert(Business business) {
        this.businessDao.insert(business);
        return business;
    }

    /**
     * 修改数据
     *
     * @param business 实例对象
     * @return 实例对象
     */
    @Override
    public Business update(Business business) {
        this.businessDao.update(business);
        return this.queryById(business.getBusinessId());
    }

    /**
     * 通过主键删除数据
     *
     * @param businessId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer businessId) {
        return this.businessDao.deleteById(businessId) > 0;
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param business 实例对象
     * @return 对象列表
     */
    @Override
    public PageInfo queryAll(int pageNo, int pageSize, Business business) {

        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<Business>(this.businessDao.queryAll(business));
    }

    /**
     * 通过主键批量删除数据
     *
     * @param businessIds 主键
     * @return 影响行数
     */
    @Override
    public int batchDeleteById(String businessIds) {

        return businessDao.batchDeleteById(businessIds);
    }

    /**
     * 按年月查询图标显示数据
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Map> queryShowChartData(int year, int month) {
        return businessDao.queryShowChartData(year,month);
    }

    @Override
    public Map<String, Object> progressEchartName(String startTime, String endTime, String progressType) {
        List<Map<String, Object>> maps = businessDao.progressEchartName(startTime, endTime, progressType);
        Map<String, Object> map = new HashMap<>();
        //定义x轴
        String[] proxyX = new String[maps.size()];
        //定义y轴
        Integer[] progressCountY = new Integer[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            proxyX[i] = maps.get(i).get("proxy").toString();
            progressCountY[i] = Integer.parseInt(maps.get(i).get("progressCount").toString());
        }
        map.put("proxyX", proxyX);
        map.put("progressCountY", progressCountY);
        return map;
    }

    @Override
    public Map<String, Object> progressEchartTime(String loginName, String progressType) {
        List<Map<String, Object>> maps = businessDao.progressEchartTime(loginName, progressType);
        Map<String, Object> map = new HashMap<>();
        //定义x轴
        String[] monthX = new String[maps.size()];
        //定义y轴
        Integer[] progressCountY = new Integer[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            monthX[i] = maps.get(i).get("month").toString();
            progressCountY[i] = Integer.parseInt(maps.get(i).get("progressCount").toString());
        }
        map.put("monthX", monthX);
        map.put("progressCountY", progressCountY);
        return map;
    }
    @Override
    public Map<String, Object> showVisitEchartName(String startTime, String endTime) {
        List<Map<String, Object>> maps = businessDao.showVisitEchartName(startTime, endTime);
        //定义返回的map
        Map<String, Object> map = new HashMap<>();
        //定义x轴
        String[] loginNameX = new String[maps.size()];
        //定义y轴
        String[] visitCountY = new String[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            loginNameX[i] = maps.get(i).get("login_name").toString();
            Object temp = maps.get(i).get("visit_count");
            if (temp == null) {
                temp = 0;
            }
            visitCountY[i] = temp.toString();
        }
        map.put("loginNameX", loginNameX);
        map.put("visitCountY", visitCountY);
        return map;
    }

    @Override
    public Map<String, Object> showVisitEchartTime(String loginName) {
        List<Map<String, Object>> maps = businessDao.showVisitEchartTime(loginName);
        //定义返回的map
        Map<String, Object> map = new HashMap<>();
        //定义x轴
        String[] monthX = new String[maps.size()];
        //定义y轴
        String[] visitCountY = new String[maps.size()];
        for (int i = 0; i < maps.size(); i++) {
            monthX[i] = maps.get(i).get("month").toString();
            Object temp = maps.get(i).get("visit_count");
            if (temp == null) {
                temp = 0;
            }
            visitCountY[i] = temp.toString();
        }
        map.put("monthX", monthX);
        map.put("visitCountY", visitCountY);
        return map;
    }

}
